cmake_minimum_required(VERSION 3.10)
project(TcpServer VERSION 1.0.0 LANGUAGES CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 编译选项
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pthread")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")

# 源文件列表
set(SOURCES
    code/core/main.cpp
    code/core/BaseTask.cpp
    code/core/Buffer.cpp
    code/core/Channel.cpp
    code/core/Dispatcher.cpp
    code/core/EpollDispatcher.cpp
    code/core/EventLoop.cpp
    code/core/TcpConnection.cpp
    code/core/TcpServer.cpp
    code/core/HttpServer.cpp
    code/core/ConnectionManager.cpp
    code/core/ThreadPool.cpp
    code/core/WorkerThread.cpp
    code/core/WorkerThreadPool.cpp
    code/core/block_log.cpp
    code/core/TimerHeap.cpp
    code/core/Connect.cpp
    code/core/ConnectPool.cpp
    
    # Protocol 层
    code/protocol/MessageCodec.cpp
    
    # Handler 层（业务逻辑）
    code/handler/LoginTask.cpp
    code/handler/GetFriendListTask.cpp
    code/handler/FileUploadTask.cpp
    code/handler/FileUploadDataTask.cpp
    code/handler/ChatMessageTask.cpp
)

# 头文件列表（可选，用于IDE显示）
set(HEADERS
    code/core/BaseTask.h
    code/core/Buffer.h
    code/core/Channel.h
    code/core/Dispatcher.h
    code/core/EpollDispatcher.h
    code/core/EventLoop.h
    code/core/TcpConnection.h
    code/core/TcpServer.h
    code/core/HttpServer.h
    code/core/ConnectionManager.h
    code/core/ThreadPool.h
    code/core/WorkerThread.h
    code/core/WorkerThreadPool.h
    code/core/block_log.h
    code/core/block_queue.h
    code/core/TimerHeap.h
    code/core/Log.h
    code/core/public.h
    code/core/Connect.h
    code/core/ConnectPool.h
    
    # Protocol 层
    code/protocol/protocol.h
    code/protocol/MessageCodec.h
    code/protocol/json.hpp
    
    # Handler 层
    code/handler/LoginTask.h
    code/handler/GetFriendListTask.h
    code/handler/FileUploadTask.h
    code/handler/FileUploadDataTask.h
    code/handler/ChatMessageTask.h
)

# 包含目录
include_directories(
    ${CMAKE_SOURCE_DIR}/code/core
    ${CMAKE_SOURCE_DIR}/code/protocol
    ${CMAKE_SOURCE_DIR}/code/handler
    ${CMAKE_SOURCE_DIR}/code/utils/cpp-httplib
    ${CMAKE_SOURCE_DIR}
)

# 创建可执行文件
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})

# 查找 MySQL Connector/C++ 库
find_library(MYSQLCPPCONN mysqlcppconn)

if(NOT MYSQLCPPCONN)
    message(FATAL_ERROR 
        "MySQL Connector/C++ 库未找到！\n"
        "请安装 MySQL Connector/C++:\n"
        "  Ubuntu/Debian: sudo apt-get install libmysqlcppconn-dev\n"
        "  或从官网下载: https://dev.mysql.com/downloads/connector/cpp/\n")
endif()

# 链接库
target_link_libraries(${PROJECT_NAME} PRIVATE 
    pthread
    ${MYSQLCPPCONN}
)

# 设置输出目录
set_target_properties(${PROJECT_NAME} PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)

# 安装规则（可选）
install(TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION bin
)

# 显示构建信息
message(STATUS "Project: ${PROJECT_NAME}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C++ standard: ${CMAKE_CXX_STANDARD}")

